package com.informa.utils;

import javax.servlet.http.HttpServletRequest;

import org.restlet.Client;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.data.Status;

public class UrlUtils {

	private static final String SLASH_STRING = "/";
	private static final String ENCODED_SLASH_STRING = "_slash_";
	private static final String COMMA_STRING = ",";
	private static final String ENCODED_COMMA_STRING = "_comma_";
	public static final String MATRIX_PARAMETER_DELIMITER = ";";
	private static final String ENCODED_SPACE = "%20";

	public static String getBasePath(HttpServletRequest request) {
		
		if (request == null) return "";
		
		String scheme = request.getScheme();
		String serverName = request.getServerName();
		String port = "" + request.getServerPort();
		String context = request.getContextPath();
		String basePath = scheme + "://" + serverName + ":" + port + context + "/";
		
		return basePath;
	}
	
	public static String getServletPath(HttpServletRequest request) {
		String basePath = getBasePath(request);
		String servletPath = basePath + request.getServletPath().replaceAll("/", "") + "/";
		return servletPath;
	}
	
    public static String getCurrentUrl(HttpServletRequest request) {
    	return getCurrentUrl(request, false);
    }
    
    public static String getCurrentUrl(HttpServletRequest request, boolean isToLowerCase) {
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String params = request.getQueryString();
        String url = scheme + "://" + serverName + ":" + port +  request.getRequestURI();

        if ( params != null ) {
            url += "?" + params;
        }
        
        if (isToLowerCase) {
        	url = url.toLowerCase();
        }
        
        return url;
    }

    public static String getCurrentRequestUrl(HttpServletRequest request) {

        String url = request.getRequestURI();
        String params = request.getQueryString();

        if ( params != null ) {
            url += "?" + params;
        }
        return url;
        
      }

	public static String encodeForUrl(String plainText) {
		String encoded = plainText
									.replaceAll(SLASH_STRING, ENCODED_SLASH_STRING)
									.replaceAll(COMMA_STRING, ENCODED_COMMA_STRING);
		
		return encoded;
	}

	public static String decodeFromUrl(String encodedText) {
		String decoded = encodedText
									.replaceAll(ENCODED_SLASH_STRING, SLASH_STRING)
									.replaceAll(ENCODED_COMMA_STRING, COMMA_STRING);
							
		return decoded;
	}

	public static String encodeAsUrl(String decoded) {
		String encoded = decoded.replaceAll(" ", ENCODED_SPACE);
		return encoded;
	}
	
	public static boolean isOk(String url) {
		return hasStatus( url, Status.SUCCESS_OK );
	}
	
	public static boolean isSuccess(String url) {
		return Status.isSuccess( getStatus( url ).getCode() );
	}

	
	public static boolean hasStatus(String url, Status status) {
		return getStatus( url ).equals( status );
	}
	
	public static Status getStatus(String url) {
		
		Response response = null;
		
		try {
			Client client = new Client(Protocol.HTTP);
			response = client.get(url);
			Status status = response.getStatus();
			
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if ( (response != null) && (response.getEntity() != null)) {
				response.getEntity().release();
			}
		}
	}
    
}
